package cn.seaboot.admin.other.web;

import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.admin.other.bean.Bulletin;
import cn.seaboot.admin.other.service.BulletinService;
import cn.seaboot.admin.user.context.UserContext;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * 系统公告 [t_sys_bulletin] - ProclamationCtrl
 *
 * @author postm
 */
@Controller
@Tag(name = "系统公告 [t_sys_bulletin] 管理")
@RequestMapping("sys/bulletin")
public class BulletinCtrl {

    @Resource
    private BulletinService bulletinService;

    @Resource
    private UserContext userContext;

    /**
     * 增
     *
     * @param pojo pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统公告 [t_sys_bulletin] 增")
    @RequestMapping(value = "info", method = RequestMethod.POST)
    public int saveInfo(Bulletin pojo) throws IOException {
        pojo.setUserId(userContext.getUserId());
        return bulletinService.insert(pojo);
    }

    /**
     * 按ID修改
     *
     * @param id      ID
     * @param content 内容
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统公告 [t_sys_bulletin] 改")
    @RequestMapping(value = "info", method = RequestMethod.PUT)
    public int saveInfo(@Params String id, @Params String content) {
        return bulletinService.updateById(id, content);
    }

    /**
     * 删
     *
     * @param id id
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "系统公告 [t_sys_bulletin] 删")
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteInfo(@Params String id) {
        return bulletinService.deleteById(id);
    }

    /**
     * 查
     *
     * @param id id
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "系统公告 [t_sys_bulletin] 查")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public Bulletin queryById(@Params String id) {
        return bulletinService.queryById(id);
    }

    /**
     * 查列表
     *
     * @param params 参数
     * @return PageInfo
     */
    @ResponseBody
    @Operation(description = "系统公告 [t_sys_bulletin] 列表查询")
    @Parameters({
            @Parameter(name = "page", description = "页号"),
            @Parameter(name = "limit", description = "行数")
    })
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<Bulletin> queryPage(@RequestParam Map<String, Object> params) {
        params.put("userId", userContext.getUserId());
        return Pagination.apply(params, map-> bulletinService.queryList(map));
    }

    /**
     * 首页系统公告 查
     *
     * @return Bulletin
     */
    @ResponseBody
    @Operation(description = "首页系统公告 查")
    @RequestMapping(value = "last", method = RequestMethod.GET)
    public Bulletin getNotice(@Params Integer bulletinType) {
        return bulletinService.selectLastByType(bulletinType);
    }
}
